package com.ying.service.order.impl;

import cn.hutool.core.collection.CollUtil;
import com.ying.core.exception.CustomException;
import com.ying.core.page.PageData;
import com.ying.core.page.PageWrap;
import com.ying.core.response.ResultCode;
import com.ying.dao.order.SellerRepository;
import com.ying.po.order.Seller;
import com.ying.service.order.SellerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
@Transactional
public class SellerServiceImpl implements SellerService {

    @Autowired
    private SellerRepository sellerRepository;

    @Override
    public Integer create(Seller seller) {
        if (seller.getId() != null) {
            Optional<Seller> optional = sellerRepository.findById(seller.getId());
            if (optional.isPresent()) {
                throw new CustomException(ResultCode.DATA_ALREADY_EXISTED, "SellerServiceImpl.create");
            }
        }
        seller.setCreateTime(new Date());
        return sellerRepository.save(seller).getId();
    }

    @Override
    public void deleteById(Integer id) {
        // 参数检验----------------------------------
        if (id == null)
            throw new CustomException(ResultCode.PARAM_IS_BLANK, "SellerServiceImpl.deleteById");
        Optional<Seller> optional = sellerRepository.findById(id);
        if (!optional.isPresent()) {
            throw new CustomException(ResultCode.RESULE_DATA_NONE, "SellerServiceImpl.deleteById");
        }
        // ------------开始业务----------------------
        Seller seller = optional.get();
        seller.setIsDelete(1);
        sellerRepository.save(seller);
    }

    @Override
    public void deleteByIdInBatch(List<Integer> ids) {
        // 参数检验----------------------------------
        if (CollUtil.isEmpty(ids))
            throw new CustomException(ResultCode.PARAM_IS_BLANK, "SellerServiceImpl.deleteByIdInBatch");
        // ------------开始业务----------------------
        for (Integer id: ids) {
            this.deleteById(id);
        }
    }

    @Override
    public void updateById(Seller seller) {
        // -------------------参数检验---------------------
        if (seller.getId() == null) {
            throw new CustomException(ResultCode.PARAM_IS_BLANK, "SellerServiceImpl.updateById");
        }
        Optional<Seller> optional = sellerRepository.findById(seller.getId());
        if (!optional.isPresent()) {
            throw new CustomException(ResultCode.RESULE_DATA_NONE, "SellerServiceImpl.updateById");
        }
        // ------------开始业务----------------------
        sellerRepository.save(seller);
    }

    @Override
    public void updateByIdInBatch(List<Seller> sellers) {
        // ------------------参数检验---------------
        if (CollUtil.isEmpty(sellers))
            throw new CustomException(ResultCode.PARAM_IS_BLANK, "SellerServiceImpl.updateByIdInBatch");
        // ------------开始业务----------------------
        for (Seller seller: sellers) {
            this.updateById(seller);
        }
    }

    @Override
    public Seller findById(Integer id) {
        // ------------------参数检验---------------
        if (id == null)
            throw new CustomException(ResultCode.PARAM_IS_BLANK, "SellerServiceImpl.findById");
        // ------------开始业务----------------------
        Optional<Seller> optional = sellerRepository.findByIdAndIsNotDelete(id);
        return optional.orElse(null);
    }

    @Override
    public List<Seller> findByIdInBatch(List<Integer> ids) {
        // ------------------参数检验---------------
        if (CollUtil.isEmpty(ids))
            return new ArrayList<Seller>();
        // ------------开始业务----------------------
        List<Seller> sellerList = new ArrayList<Seller>();
        for (Integer id : ids) {
            sellerList.add(this.findById(id));
        }
        return sellerList;
    }

    @Override
    public PageData<Seller> findAllByPage(PageWrap pageWrap, Integer userId) {
        // ------------------参数检验---------------
        if (!pageWrap.check()) {
            throw new CustomException(ResultCode.PARAM_IS_BLANK, "SellerServiceImpl.findPage");
        }
        // ------------开始业务----------------------
        Pageable pageable = PageRequest.of(pageWrap.getPage(), pageWrap.getCapacity(), pageWrap.sortMethod());
        Page<Seller> sellerPage = sellerRepository.getSellerPageIsNotDeleted(pageable, userId);
        PageData<Seller> pageData = PageData.from(sellerPage);
        return pageData;
    }

    @Override
    public long countAll(Integer userId) {
        List<Seller> all = this.findAll(userId);
        return all.size();
    }

    @Override
    public List<Seller> findAll(Integer userId) {
        return sellerRepository.findAllIsNotDelete(userId);
    }

    @Override
    public PageData<Seller> queryAllByPage(PageWrap<Seller> pageWrap) {
        // ------------------参数检验---------------
        if (!pageWrap.check()) {
            throw new CustomException(ResultCode.PARAM_IS_BLANK, "SellerServiceImpl.findPage");
        }
        // ------------开始业务----------------------
        Pageable pageable = PageRequest.of(pageWrap.getPage(), pageWrap.getCapacity(), pageWrap.sortMethod());
        Seller seller = pageWrap.getModel();
        // 待实现
        String n = seller.getName();
        String p = seller.getProvince();
        String m = seller.getPhone();
        Integer u = seller.getUserId();
        Page<Seller> sellerPage = null;
        if (n == null && p == null && m == null) {
            sellerPage = sellerRepository.getSellerPageIsNotDeleted(pageable, u);
        } else if (n != null && p == null && m == null) {
            sellerPage = sellerRepository.getPageByName(pageable, n, u);
        } else if (n == null && p != null && m == null) {
            sellerPage = sellerRepository.getPageByProvince(pageable, p, u);
        } else if (n == null && p == null && m != null) {
            sellerPage = sellerRepository.getPageByPhone(pageable, m, u);
        } else if (n != null && p != null && m == null) {
            sellerPage = sellerRepository.getPageByNameAndProvince(pageable, n, p, u);
        } else if (n != null && p == null && m != null) {
            sellerPage = sellerRepository.getPageByNameAndPhone(pageable, n, m, u);
        } else if (n == null && p != null && m != null) {
            sellerPage = sellerRepository.getPageByProvinceAndPhone(pageable, p, m, u);
        } else if (n != null && p != null && m != null) {
            sellerPage = sellerRepository.getPageByNameAndProvinceAndPhone(pageable, n, p, m, u);
        }
        PageData<Seller> pageData = PageData.from(sellerPage);
        return pageData;
    }

    @Override
    public List<String> queryProvince(Integer userId) {
        List<String> province = new ArrayList<>();
        List<Seller> all = sellerRepository.findAll();
        for (Seller seller : all) {
            if (seller.getIsDelete()==0 && Objects.equals(seller.getUserId(), userId) && !province.contains(seller.getProvince())) {
                province.add(seller.getProvince());
            }
        }
        province.remove(null);
        return province;
    }

    @Override
    public List<Map> getAllBuyerName(Integer userId) {
        List<Seller> sellerList = this.findAll(userId);
        List<String> sellerNames = new ArrayList<>();
        List<Map> mapList = new ArrayList<Map>();
        for (Seller seller : sellerList) {
            if (!sellerNames.contains(seller.getName()) && seller.getType() ==0) {
                sellerNames.add(seller.getName());
                Map sellerNameAndIdsMap = new HashMap();
                sellerNameAndIdsMap.put("id", seller.getId());
                sellerNameAndIdsMap.put("name", seller.getName());
                mapList.add(sellerNameAndIdsMap);
            }
        }
        return mapList;
    }

    @Override
    public Integer getBuyerCount(Integer userId) {
        return sellerRepository.getBuyerCount(userId);
    }

    @Override
    public List<Integer> getBuyerIds(Integer userId) {
        return sellerRepository.findBuyerIds(userId);
    }

    @Override
    public List<Seller> findNewBuyerThisYear(Integer userId, Integer year) {
        return sellerRepository.findNewBuyerThisYear(userId, year);
    }


}
